﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace publish_test
{
    /// <summary>
    /// ImageHandler 的摘要说明
    /// </summary>
    public class ImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var action=context.Request["action"];
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(action);
            if (methodInfo != null)
            {
                //在这里调用请求的action
                methodInfo.Invoke(this, new object[] { context });
            }

          
        }
        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="context"></param>
        public void GetList(HttpContext context)
        {
           
            using (System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap(300,300))
            {
                using (System.Drawing.Graphics gc = System.Drawing.Graphics.FromImage(bitmap))
                {
                    gc.DrawString("ip地址是:" + context.Request.UserHostAddress, new System.Drawing.Font("宋体", 12), System.Drawing.Brushes.Red, 0, 0);
                    gc.DrawString("操作系统是:" + context.Request.Browser.Platform, new System.Drawing.Font("宋体", 12), System.Drawing.Brushes.Yellow, 0, 50);
                    gc.DrawString("浏览器信息:" + context.Request.Browser.Type, new System.Drawing.Font("宋体", 12), System.Drawing.Brushes.Yellow, 0,100);
                }
                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            
          //  context.Response.Write("returns jsonData");
        }
        /// <summary>
        /// 获取div图片
        /// </summary>
        /// <param name="context"></param>
        public void GetCe(HttpContext context) {
            var path = HttpContext.Current.Server.MapPath("3100.jpg");
            context.Response.ContentType = "Image/JPEG";
          
            string name = context.Request["name"];
            using (Bitmap map=new Bitmap(path))
            {
                using (Graphics g=Graphics.FromImage(map))
                {
                    g.DrawString(name, new Font("仿宋", 12), Brushes.Red, 0, 0);
                }
                map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="context"></param>
        public void Getimag(HttpContext context)
        {
            var path = HttpContext.Current.Server.MapPath("3100.jpg");
            context.Response.ContentType = "Image/JPEG";
            string encodeFileName = HttpUtility.UrlEncode("张钧甯.jpg");
            context.Response.AddHeader("content-Disposition", string.Format("attachment; filename ={0}", encodeFileName));//这句很重要,可以决定文件的保存位置
            string name = context.Request["name"];
            using (Bitmap map = new Bitmap(path))
            {
                using (Graphics g = Graphics.FromImage(map))
                {
                    g.DrawString(name, new Font("仿宋", 12), Brushes.Red, 0, 0);
                }
                map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        /// <summary>
        /// 下载txt
        /// </summary>
        /// <param name="context"></param>
        public void GetFile(HttpContext context)
        {
            string encodeFileName = HttpUtility.UrlEncode("过滤词.txt");//默认下载文件名称
            context.Response.AddHeader("content-Disposition", string.Format("attachment; filename ={0}", encodeFileName));
            var path = HttpContext.Current.Server.MapPath("1.txt");
            //context.Response.ContentType = "Image/JPEG";
            context.Response.WriteFile("1.txt");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}